package com.m1.tzolkin.model.board;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;

import com.m1.tzolkin.model.exceptions.StepMoveException;
import com.m1.tzolkin.model.players.ITZPlayer;
import com.m1.tzolkin.model.temples.ITemple;
import com.m1.tzolkin.model.temples.ITemplesManager;
import com.m1.tzolkin.model.temples.TemplesManager;

public class TempleBoard extends BasicBoard implements ITempleBoard {
	
	// ATTRIBUTS
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5460186289918755682L;
	private final ITemplesManager templesManager;
	
	// CONSTRUCTEUR
	
	public TempleBoard(Collection<ITZPlayer> players) {
		super(players);
		templesManager = new TemplesManager();
		for (ITZPlayer p : players) {
			templesManager.addKey(p, DEFAULT_TEMPLE_MARCH);
		}
		templesManager.addPropertyChangeListener(new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent arg0) {
				listeners.firePropertyChange("templeManager", null, null);
			}
		});
	}
	
	// REQUETES
	
	@Override
	public int getTemplePosition(ITemple t, ITZPlayer p) {
		return templesManager.getPosition(t, p);
	}
	
	// COMMANDES
	
	@Override
	public void upTemple(ITZPlayer p, ITemple t) throws StepMoveException {
		if (p == null || t == null) {
			throw new IllegalArgumentException();
		}
		int oldStep = templesManager.getPosition(t, p);
		templesManager.up(t, p);
		listeners.firePropertyChange("upTemple", oldStep, templesManager.getPosition(t, p));
		getLog().addSentence(new String[] {
			p.getName(), "CLIMBS", "1", "STEP", "ON", t.toString()	
		});
	}
	
	@Override
	public void downTemple(ITZPlayer p, ITemple t) throws StepMoveException {
		if (p == null || t == null) {
			throw new IllegalArgumentException();
		}
		int oldStep = templesManager.getPosition(t, p);
		templesManager.down(t, p);
		listeners.firePropertyChange("upTemple", oldStep, templesManager.getPosition(t, p));
		getLog().addSentence(new String[] {
			p.getName(), "STEP_DOWN", "OF", "1", "ON", t.toString()	
		});
	}
	
	// OUTILS
	
	protected ITemplesManager getTemplesManager() {
		return templesManager;
	}

}
